Java By Comparison: Become a Java Craftsman in 70 Examples by Simon Harrer & Jörg Lenhard & Linus Dietz

Java By Comparison: Become a Java Craftsman in 70 Examples by Simon Harrer & Jörg Lenhard & Linus Dietz

Author:Simon Harrer & Jörg Lenhard & Linus Dietz [Harrer, Simon]
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Published: 2018-03-22T00:00:00+00:00


Avoid Breaking the Cause Chain

class TransmissionParser {

static Transmission parse(String rawMessage) {

if (rawMessage != null

&& rawMessage.length() != Transmission.MESSAGE_LENGTH) {

throw new IllegalArgumentException(

String.format("Expected %d, but got %d characters in '%s'",

Transmission.MESSAGE_LENGTH, rawMessage.length(),

rawMessage));

}

String rawId = rawMessage.substring(0, Transmission.ID_LENGTH);

String rawContent = rawMessage.substring(Transmission.ID_LENGTH);

try {

int id = Integer.parseInt(rawId);

String content = rawContent.trim();

return new Transmission(id, content);

} catch (NumberFormatException e) {

» throw new IllegalArgumentException(

String.format("Expected number, but got '%s' in '%s'",

rawId, rawMessage));

}

}

}

Exceptions can cause more exceptions. When an exception is caught but cannot be handled, it must be rethrown. If a bug is involved, this can propagate until the program crashes. If the error handling was done right, you will be presented with a stack trace that represents the cause chain—a list where each exception is linked to the one that caused it. When tracking down a bug, a detailed cause chain is worth a pile of gold.

That’s why you need to make sure that you never break this chain. For instance, look at the code above. It catches the NumberFormatException and throws a new IllegalArgumentException with an informative message instead. No bad exception handling as such—but it breaks the cause chain!

Can you spot the problem? It’s the IllegalArgumentException not getting the reference to its cause, the NumberFormatException. Without this link there’s no cause chain. When you look at the stack trace of this IllegalArgumentException, you won’t find a hint that it stems from a NumberFormatException or from what line in the code. We lost a lot of useful information—the NumberFormatException has its own message that provides more information and context on another level of abstraction and its own stack trace with line numbers.

So how can we keep the cause chain instead of breaking it?

class TransmissionParser {

static Transmission parse(String rawMessage) {

if (rawMessage != null

&& rawMessage.length() != Transmission.MESSAGE_LENGTH) {

throw new IllegalArgumentException(

String.format("Expected %d, but got %d characters in '%s'",

Transmission.MESSAGE_LENGTH, rawMessage.length(),

rawMessage));

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.